package com.eco.fanliapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.dao.AdministratorMapper;
import com.eco.fanliapp.dao.SyAdminLoginIpMapper;
import com.eco.fanliapp.entity.Administrator;
import com.eco.fanliapp.entity.SyAdminLoginIp;
import com.eco.fanliapp.service.ISyAdminLoginIpService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SyAdminLoginIpServiceImpl extends ServiceImpl<SyAdminLoginIpMapper, SyAdminLoginIp> implements ISyAdminLoginIpService {
    @Autowired
    private SyAdminLoginIpMapper syAdminLoginIpMapper;
    @Autowired
    private AdministratorMapper administratorMapper;
    @Override
    public ServerResponse loginIpList(Integer pageNum, Integer pageSize, Integer companyId,String keyWord) {
        PageHelper.startPage(pageNum,pageSize);
        List<SyAdminLoginIp>  syAdminLoginIpList = new ArrayList<>();
        if(companyId == 1){
            syAdminLoginIpList = syAdminLoginIpMapper.selectIpList(null,keyWord);
        }else{
            syAdminLoginIpList = syAdminLoginIpMapper.selectIpList(companyId,keyWord);
        }
        return ServerResponse.createBySuccess("查询成功",new PageInfo<>(syAdminLoginIpList));
    }

    @Override
    public ServerResponse saveAndUpdate(SyAdminLoginIp syAdminLoginIp) {
        if(syAdminLoginIp.getTableId() == null){
            syAdminLoginIp.setCreateTime(new Date());
            int insert = syAdminLoginIpMapper.insert(syAdminLoginIp);
            if(insert>0){
                return ServerResponse.createBySuccessMessage("新增成功");
            }else {
                return ServerResponse.createByErrorMessage("新增失败");
            }
        }else{
            syAdminLoginIp.setUpdateTime(new Date());
            int i = syAdminLoginIpMapper.updateById(syAdminLoginIp);
            if(i>0){
                return ServerResponse.createBySuccessMessage("修改成功");
            }else {
                return ServerResponse.createByErrorMessage("修改失败");
            }
        }
    }

    public ServerResponse delete(Integer tableId){
        int i = syAdminLoginIpMapper.deleteById(tableId);
        if(i>0){
            return ServerResponse.createBySuccessMessage("删除成功");
        }else {
            return ServerResponse.createByErrorMessage("删除失败");
        }
    }

    @Override
    public List<String> loginIpList(Integer companyId) {
        QueryWrapper qw =new QueryWrapper();
        qw.eq("company_id",companyId);
        qw.eq("deleted",0);
        qw.eq("enable",1);
        List<SyAdminLoginIp> list = syAdminLoginIpMapper.selectList(qw);
        List<String> ipList = new ArrayList<>();
        for(SyAdminLoginIp l1:list){
            ipList.add(l1.getIp());
        }
        return ipList;
    }
}
